package io.github.simplejy.miodemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import io.github.simplejy.miodemo.R;
import io.github.simplejy.miodemo.adapter.HistoryAdapter;
import io.github.simplejy.miodemo.bean.HistoryParamData;

/**
 * 历史参数
 */

public class HistoryParamActivity extends AppCompatActivity implements HistoryAdapter.OnClickHistoryListener {

    private ListView list;
    private ArrayList<HistoryParamData> historyDatas;
    private HistoryParamData historyDataOne, historyDataTwo, historyDataThree;
    //编辑弹出框
    private AlertDialog dialog;
    //详情弹出框
    private AlertDialog detailDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_param);

        initView();
    }

    private void initView() {

        list = (ListView) findViewById(R.id.list_history);
        findViewById(R.id.layout_back_main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.tv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showEditDialog();
            }
        });
        setData();


        HistoryAdapter adapter = new HistoryAdapter(this, historyDatas);
        adapter.setOnClickHistoryListener(this);
        list.setAdapter(adapter);

    }

    private void setData() {
        //模拟数据
        historyDataOne = new HistoryParamData("2016-11-02");
        historyDataTwo = new HistoryParamData("2017-01-17");
        historyDataThree = new HistoryParamData("2017-02-01");

        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");
        historyDataOne.addItem("46164f6a4g6a4g6ad4g6a");

        historyDataTwo.addItem("wqiryqiwry19421401284");

        historyDataThree.addItem("30asais1flflafjsjqfl");

        historyDatas = new ArrayList<>();

        historyDatas.add(historyDataThree);
        historyDatas.add(historyDataTwo);
        historyDatas.add(historyDataOne);
    }

    private void showEditDialog() {
        if (dialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View v = getLayoutInflater().inflate(R.layout.dialog_edit_history_param, null);
            //设置dialog里的控件
            //
            Button btnCancel = (Button) v.findViewById(R.id.btn_cancel);
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            Button btnSure = (Button) v.findViewById(R.id.btn_sure);
            btnSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            //
            //
            builder.setView(v);
            dialog = builder.create();
        }
        dialog.show();
    }

    private Button btnUse, btnClose, btnDelete;
    private TextView tvBrand, tvType;

    @Override
    public void onClickHistory(String item) {
        if (detailDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View v = getLayoutInflater().inflate(R.layout.dialog_history_detail, null);
            btnClose = (Button) v.findViewById(R.id.btn_close);
            btnDelete = (Button) v.findViewById(R.id.btn_delete);
            btnUse = (Button) v.findViewById(R.id.btn_use);
            tvBrand = (TextView) v.findViewById(R.id.tv_brand);
            tvType = (TextView) v.findViewById(R.id.tv_type);

            btnClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    detailDialog.cancel();
                }
            });
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(HistoryParamActivity.this, "删除", Toast.LENGTH_SHORT).show();
                }
            });
            btnUse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(HistoryParamActivity.this, "使用", Toast.LENGTH_SHORT).show();
                }
            });
            builder.setView(v);
            detailDialog = builder.create();
        }

        tvType.setText("参数:" + item);
        detailDialog.show();


    }
}
